import { fetchUserInfoById, getUserInfo } from '@/axios/api/user'
import { getToken, setUserInfo } from '@/utils/webStorage'
import { useQuery, useInfiniteQuery, useMutation } from '@tanstack/vue-query'
import {
  getUserFansList,
  getUserFollowList,
  fetchUserTopicList,
  fetchUserLeftTopicList,
  followUser,
  cancelFollowUser,
  updateUserInfo,
  getHotUpper,
} from '@/axios/api/user'
import { isLastPage } from '@/utils/dataUtils'


// 我的信息
export function useMyInfoQuery() {
  const { data: userInfo, refetch } = useQuery({
    queryKey: ['userInfo'],
    queryFn: getUserInfo,
    enabled: !!getToken(), // 只在有token时执行查询
    retry: 1, // 失败时重试1次
    cacheTime: 0,
    staleTime: 0
  })

  return {
    userInfo,
    refetchUserInfo: refetch,
  }
}

// 用户个人中心信息
export const useUserInfoQuery = (userId) => {
  const { data: userInfo, refetch } = useQuery({
    queryKey: ['userInfoById', userId],
    queryFn: fetchUserInfoById,
    enabled: !!userId,
  })

  return { userInfo, refetch }
}

// 用户粉丝列表
export const useUserFansListQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['userFansList', uid],
    queryFn: ({ pageParam = 1 }) => {
      return getUserFansList({ uid: uid.value, page: pageParam })
    },
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.current_page + 1
    },
    initialPageParam: 1,
  })
}

// 用户关注列表
export const useUserFollowListQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['userFollowList', uid],
    queryFn: ({ pageParam = 1 }) => {
      return getUserFollowList({ uid: uid.value, page: pageParam })
    },
    getNextPageParam: (lastPage) => {
      if (lastPage.current_page < lastPage.total) {
        return lastPage.current_page + 1
      }
      return undefined
    },
    initialPageParam: 1,
  })
}

// 用户加入的主题
export const useUserTopicListQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['fetchUserTopicList', uid],
    queryFn: ({ pageParam = 1 }) => {
      return fetchUserTopicList({ uid, page: pageParam })
    },
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.current_page + 1
    },
    enabled: !!getToken(),
    initialPageParam: 1,
  })
}


// 用户加入的主题对应左侧的列表--区分其他接口
export const useUserLeftTopicListQuery = (uid) => {
  return useInfiniteQuery({
    queryKey: ['fetchUserLeftTopicList', uid],
    queryFn: ({ pageParam = 1 }) => {
      return fetchUserLeftTopicList({ uid, page: pageParam })
    },
    getNextPageParam: (lastPage) => {
      if (isLastPage(lastPage)) {
        return undefined
      }
      return lastPage.current_page + 1
    },
    enabled: !!getToken(),
    initialPageParam: 1,
  })
}


// 关注用户
export const useFollowMutation = (options = {}) => {
  return useMutation({
    mutationFn: followUser,
    ...options
  })
}

// 取消关注用户
export const useCancelFollowMutation = (options = {}) => {
  return useMutation({
    mutationFn: cancelFollowUser,
    ...options
  })
}

// 更新用户信息
export const useUpdateUserInfoMutation = () => {
  return useMutation({
    mutationFn: updateUserInfo,
    onSuccess: () => {},
  })
}

// 获取热门博主
export const useHotUpperQuery = () => {
  return useQuery({
    queryKey: ['hotUpper'],
    queryFn: getHotUpper,
    select: (data) => {
      return {
        ...data,
        result: data.result.map((item) => ({
          ...item,
          hasFollow: item.isFollow,
        })),
      }
    },
  })
}
